<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Create_meal extends CI_Controller {
	public function index()
	{
            $this->load->model('categories');
            $categories = $this->categories->getAll();
            
            $data['title'] = "Create Meal";
            $data['data'] = array (
                "title"=>"Create Meal",
                "categories" => $categories);
            $data['js'] = array("create_meal.js");
            $data['view'] = "meal_editing/view_create_meal";
            $this->load->view('view_template',$data);
	}
        public function searchIngredients(){
            $this->load->model("ingredients");
            $post = $this->ajax->post('request_string');
            $ingredients = $this->ingredients->getPartial($post['request_string']);
            
            $return = array();
            
            foreach($ingredients->result() as $i){
                $return[] = array("value" => $i->ingredient_title, "label" => $i->ingredient_id);
            }
            
            $return[] = array(
                "value" => '<i>Create New Ingredient</i>',
                'label' => -1
                );
            //This is how jquery ui requires the format for the autocomplete
            echo json_encode($return);
        }
        
        public function postMeal(){
            extract($this->ajax->post('meal_title','meal_id','categories','ingredients','description'));
            $this->load->model('meals');
            
            
            if ($this->meals->isToday($meal_id)){
                $this->session->set_flashdata('meal_response','Cannot modify a meal which is allocated for today.');
                echo json_encode("today");
                return;
            }
            
            $error = array();
            $meal_temp = $this->meals->getbyTitle(trim($meal_title));
            if ($meal_temp != null){
                if ($meal_temp->meal_id != $meal_id){
                    $error['title'] = "Meal title is not unique";
                }
            }
            
            if (trim($meal_title) == ""){
                    $error['title'] = "Meal title is empty";
            }
            if (!is_array($categories)){
                $error['categories'] = "Add at least one category";
            }
            if (!is_array($ingredients)){
                $error['ingredients'] = "Add at least one ingredient";
            }
            
            if ( count($error) > 0){
                echo json_encode($error);
                return;
            }
            
            //If not a meal already we are creating.
            if ($meal_id < 1){
                if ($this->meals->create(trim($meal_title),$categories,$ingredients,$description)){
                    $this->session->set_flashdata('meal_response','The meal \''.trim($meal_title).'\' was successfully created.');
                    echo json_encode("good");
                    return;
                }
            }
            else{//update
                if ($this->meals->update($meal_id,trim($meal_title),$categories,$ingredients,$description)){
                    $this->session->set_flashdata('meal_response','The meal \''.trim($meal_title).'\' was successfully updated.');
                    echo json_encode("good");
                    return;
                }
            }
            $this->session->set_flashdata('meal_response','Server error, no changes have been made.');
            echo json_encode("server");
        }
        
        public function checkTitle(){
            extract($this->ajax->post('meal_title','meal_id'));
            $this->load->model('meals');
            
            $returnString="good";
            
            $meal = $this->meals->getByTitle($meal_title);
                        
            if ($meal != null){
                if (strtoupper($meal_title) == strtoupper($meal->meal_title)){
                    if ($meal_id != $meal->meal_id){
                        $returnString = "used";
                    }
                }
            }
            
            echo $returnString;
        }
        
        public function editMeal($meal_id){
            $this->load->model("meals");
            
            
            if ($this->meals->isToday($meal_id)){
                $this->session->set_flashdata('meal_response', 'Cannot edit a meal that is orderable today.');
                redirect('meal_editing/edit_meals');
                return;
            }
            
            $meal = $this->meals->getMealInfo($meal_id);
            $meal_ingredients = $this->meals->getIngredients($meal_id);
            $meal_categories = $this->meals->getCategories($meal_id);
            
            $this->load->model('categories');
            $categories = $this->categories->getAll();
            
            
            $data['title'] = "Edit Meal";
            $data['data'] = array (
                "title"=>"Edit Meal",
                "categories" => $categories,
                "meal_title" => $meal->meal_title,
                "meal_id" => $meal_id,
                "meal_categories" => $meal_categories,
                "meal_ingredients" => $meal_ingredients,
                "meal_description" => $meal->meal_description,
                );
            $data['js'] = array("create_meal.js");
            $data['view'] = "meal_editing/view_create_meal";
            $this->load->view('view_template',$data);
        }
        
        public function cloneMeal($meal_id){
            $this->load->model("meals");
            
            $meal = $this->meals->getMealInfo($meal_id);
            $meal_ingredients = $this->meals->getIngredients($meal_id);
            $meal_categories = $this->meals->getCategories($meal_id);
            
            $this->load->model('categories');
            $categories = $this->categories->getAll();
            
            
            $data['title'] = "Create Meal";
            $data['data'] = array (
                "title"=>"Create Meal",
                "categories" => $categories,
                "meal_title" => $meal->meal_title." - Copy",
                "meal_id" => -1,
                "meal_categories" => $meal_categories,
                "meal_ingredients" => $meal_ingredients,
                "meal_description" => $meal->meal_description,
                );
            $data['js'] = array("create_meal.js");
            $data['view'] = "meal_editing/view_create_meal";
            $this->load->view('view_template',$data);
        }
        
        public function getIngredient(){
            $ingredient_title = $this->input->post("ingredient_title");
            if ($ingredient_title == ""){
                echo "fail";
                return;
            }
            
            $this->load->model('ingredients');
            $ingredient = $this->ingredients->getByTitle(trim($ingredient_title));
            
            echo $ingredient->ingredient_id;
            
        }
}